package com.wzl.plugins.controller;

import com.wzl.common.VkPublicResponse;
import com.wzl.common.pojo.VkSmbmsUser;
import com.wzl.plugins.config.NacosConfig;
import com.wzl.plugins.config.NacosSpringContextUtil;
import com.wzl.plugins.service.NacosAppService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * Nacos测试
 *
 * @author weizhongli
 * @since 1.0, 2023/5/03 14:21
 */
@RefreshScope //此注解能刷新配置文件，但只对添加了此注解的类有效，并非全局有效
@RestController
public class NacosTestController {

    @Resource
    private NacosAppService nacosAppService;
    
    @Value("${nacos.test.message}")
    private String message;

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 测试获取nacos配置里面的信息
     * @return
     */
    @RequestMapping("/test")
    public VkPublicResponse test () {
        VkPublicResponse vkPublicResponse = new VkPublicResponse();
        NacosConfig nacosConfig = NacosSpringContextUtil.getBean(NacosConfig.class);
        String serverName = nacosConfig.getServerName();
        String dataSourceName = nacosConfig.getDataSourceName();
        String data = serverName + "---" + dataSourceName;
        vkPublicResponse.setData(data);
        return vkPublicResponse;
    }

    /**
     * 获取配置文件里面的信息
     * @return
     */
    @RequestMapping("/test1")
    public VkPublicResponse test1 () {
        VkPublicResponse vkPublicResponse = new VkPublicResponse();
//        NacosConfig nacosConfig = NacosSpringContextUtil.getBean(NacosConfig.class);
//        String message = nacosConfig.getMessage();
        vkPublicResponse.setData(message);
        return vkPublicResponse;
    }

    @RequestMapping("/test2/{id}")
    public VkPublicResponse test2 (@PathVariable("id") int id) {
        VkPublicResponse vkPublicResponse = new VkPublicResponse();
        VkSmbmsUser userPojo = nacosAppService.selectById(0);
        vkPublicResponse.setData(userPojo);
        return vkPublicResponse;
    }


    @GetMapping("get")
    public String get(){
        //通过服务名的方式调用远程服务（非ip端口）
        String result = restTemplate.getForObject(
                "https://www.csdn.net/", String.class);
        return result;
    }


}
